/**
 * Software License Declaration.
 * <p>
 * zhilingsd.com, Co,. Ltd.
 * Copyright © 2016 All Rights Reserved.
 * <p>
 * Copyright Notice
 * This documents is provided to zhilingsd contracting agent or authorized programmer only.
 * This source code is written and edited by zhilingsd Co,.Ltd Inc specially for financial
 * business contracting agent or authorized cooperative company, in order to help them to
 * install, programme or central control in certain project by themselves independently.
 * <p>
 * Disclaimer
 * If this source code is needed by the one neither contracting agent nor authorized programmer
 * during the use of the code, should contact to zhilingsd Co,. Ltd Inc, and get the confirmation
 * and agreement of three departments managers  - Research Department, Marketing Department and
 * Production Department.Otherwise zhilingsd will charge the fee according to the programme itself.
 * <p>
 * Any one,including contracting agent and authorized programmer,cannot share this code to
 * the third party without the agreement of zhilingsd. If Any problem cannot be solved in the
 * procedure of programming should be feedback to zhilingsd Co,. Ltd Inc in time, Thank you!
 */
package personal.zq.practice.common.utils;

import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 正则判断路径 win/linux
 *
 * @author tuzhen(antman)
 * @version 1.0
 * @date 2021/2/24 11:03
 */
@Slf4j
public class PathPatternUtil {

    private final static String LINUX_ROUTE_PATTERN = "^\\/?(\\w+\\-?\\/?)+$";

    private final static String WINDOWS_ROUTE_PATTERN = "^([a-zA-Z]:|([a-zA-Z]:)?\\\\[^\\/\\:\\*\\?\\\"\"\\<\\>\\|\\,]*)$";

    public static boolean judgeRoute(String path) {
        if (runningOnEnv("linux")) {
            return judgeLinuxRoute(path);
        } else if (runningOnEnv("windows")) {
            return judgeWindowRoute(path);
        }
        return false;
    }

    /**
     * 获取文件路径
     *
     * @param path  全文件路径
     * @return 返回指定字符串
     */
    private static String cutLinuxFileRoute(String path) {
        String filePath = path;
        if (StringUtils.isNotBlank(path)) {
            path = path.trim();
            //路径带有文件名
            if (path.lastIndexOf(".") > 0) {
                int fileIndex = path.lastIndexOf("/");
                filePath = path.substring(0, fileIndex);
            }
        }
        return filePath;
    }

    /**
     *  判断linux 文件路径
     * @param path 文件路径
     * @return 是否符合linux 文件路径
     */
    private static boolean judgeLinuxRoute(String path) {
        if (StringUtils.isNotBlank(path)) {
            String filePath = cutLinuxFileRoute(path);
            Pattern pathPattern = Pattern.compile(LINUX_ROUTE_PATTERN);
            return pathPattern.matcher(filePath).matches();
        }
        return false;
    }

    /**
     *  判断window 文件路径
     * @param path 文件路径，带有文件名
     * @return 是否合法
     */
    private static boolean judgeWindowRoute(String path) {
        if (StringUtils.isNotBlank(path)) {
            Pattern pattern = Pattern.compile(WINDOWS_ROUTE_PATTERN);
            return pattern.matcher(path).matches();
        }
        return false;
    }

    /**
     *  查看当前运行环境
     * @param env 当前运行环境
     * @return 是否为指定运行环境
     */
    private static boolean runningOnEnv(String env) {
        String system = System.getProperty("os.name");
        return system.toLowerCase().contains(env);
    }
}
